package ai4games_tests.soccer;

import game2dai.World;
import game2dai.entities.BaseEntity;
import game2dai.fsm.State;
import game2dai.fsm.Telegram;
import game2dai.steering.SBF;

public class PlayerChaseBall extends State {


	private static PlayerChaseBall instance = null;

	public static PlayerChaseBall state(){
		if(instance == null){
			instance = new PlayerChaseBall();
			instance.name = "Chase ball";
		}
		return instance;
	}

	@Override
	public void enter(BaseEntity owner) {
		PlayerBase player = (PlayerBase) owner;
		player.AP().seekOn(player.team.pitch.ball.pos());
	}

	@Override
	public void execute(BaseEntity owner, double deltaTime, World world) {
		PlayerBase player = (PlayerBase) owner;
		// If player in kick range change to kick ball state
		if(player.isBallWithinKickingRange()){
			player.FSM().changeState(PlayerKickBall.state());
			return;
		}
		// If this player is the closest to the ball continue chasing it
		if(player.isClosestTeamMemberToBall()){
			player.AP().seekOn(player.team.pitch.ball.pos());
			return;
		}
		// If player is not closest to ball he should return back to current region
		player.FSM().changeState(PlayerReturnToRegion.state());
	}

	@Override
	public void exit(BaseEntity owner) {
		PlayerBase player = (PlayerBase) owner;
		player.AP().seekOff();
	}

	@Override
	public boolean onMessage(BaseEntity owner, Telegram tgram) {
		return false;
	}

}
